package hu.myai.triggers.coverlong;

import hu.myai.api.tick.AbstractTickProvider;
import hu.myai.api.triggers.AbstractTrigger;
import hu.myai.indicators.pivot.LastHighIndicator;
import hu.myai.indicators.pivot.LastLowIndicator;
import hu.myai.model.IndicatorVO;
import hu.myai.model.TickVO;
import hu.myai.model.TriggerVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * If stock prize do a Zig-Zag than it usually reach 161.8%. 100% is the
 * difference between last high and the low before. It works if the market makes
 * higher low. (bull micro trend)
 * 
 * @author bozbalint
 * 
 * */
public class FibonacciExtension162 extends AbstractTrigger {

	/**
	 * The object used when logging debug,errors,warnings and info.
	 * */
	private final Logger log = LoggerFactory.getLogger(this.getClass());

	public FibonacciExtension162(String name, AbstractTickProvider tick) {
		super(name, tick);
	}

	private String lastLowID;

	public void setLastLowIndicatorParameter(double parameter) {
		lastLowID = register(new LastLowIndicator(parameter));
	}

	private String lastHighID;

	public void setLastHighIndicatorParameter(double parameter) {
		lastHighID = register(new LastHighIndicator(parameter));
	}

	@Override
	protected TriggerVO triggerCalculation() {

		IndicatorVO llIndicator = indicatorMap.get(lastLowID);
		if (llIndicator == null)
			return null;

		IndicatorVO lhIndicator = indicatorMap.get(lastHighID);
		if (lhIndicator == null)
			return null;

		// new high
		if (lhIndicator.isChanged()) {
			TriggerVO trigger = new TriggerVO();

			double minMax = lhIndicator.getValue() - llIndicator.getValue();

			// the 161.8% from the low
			double exitLevel = minMax * 1.6 + llIndicator.getValue();

			trigger.setTick(new TickVO(null, null, null, exitLevel, 0, lastTick.getTime(), lastTick.getTimestamp()));
			trigger.setRisk(10);
			trigger.setTradeType(TriggerVO.SELL_LIMIT);

			log.debug(trigger.toString());
			return trigger;
		}

		return null;
	}

}
